﻿// <copyright file="UV2ProjectDefinesParser.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Provides an project defines parser for UV2 project files..
// </summary>

namespace DoxyOutParser.CodeCheck
{
	using System;
	using System.IO;

	/// <summary>
	/// Parses an UV2 project file to find the project defines specified within it.
	/// </summary>
	public class UV2ProjectDefinesParser : IProjectDefinesParser
	{
		/// <summary>
		/// The file which needs parsing.
		/// </summary>
		private string uv2ProjectFile;

		/// <summary>
		/// Initializes a new instance of the UV2ProjectDefinesParser class.
		/// </summary>
		/// <param name="projectFile">The UV2 project file which needs parsing.</param>
		public UV2ProjectDefinesParser(string projectFile)
		{
			// TODO Check the value of string and eventually check if exist or not.
			this.uv2ProjectFile = projectFile;
		}

		#region IProjectDefinesParser Members

		/// <summary>
		/// Returns a list of every project defines specified in an UV2 project file.
		/// </summary>
		/// <returns>The project defines table.</returns>
		public string[] GetProjectDefinesTable()
		{
			// Using of StreamReader to parse the file
			StreamReader uv2ProjectStreamReader = new StreamReader(this.uv2ProjectFile);
			string line = uv2ProjectStreamReader.ReadLine();
			string[] definesSeparators = { "," };

			while (line != null)
			{
				if (line.Contains("ADSCDEFN"))
				{
					int startIndex = line.IndexOf("(", StringComparison.Ordinal);
					int endIndex = line.IndexOf(")", StringComparison.Ordinal);
					if (startIndex != 0 && endIndex != 0)
					{
						string projectDefineData = line.Substring(startIndex + 1, (endIndex - startIndex - 1));
						uv2ProjectStreamReader.Close();
						projectDefineData = projectDefineData.Replace(" ", string.Empty);
						return projectDefineData.Split(definesSeparators, StringSplitOptions.RemoveEmptyEntries);
					}
				}

				// Read Next Line
				line = uv2ProjectStreamReader.ReadLine();
			}

			return null;
		}

		#endregion
	}
}
